package com.orangelife.mobile.goodsrent.biz;

import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.JSONHelper;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by yyx
 * on 2015/11/10.
 */
public class GoodsRentBiz {

    private static GoodsRentBiz goodsRentBiz = null;

    public static GoodsRentBiz getInstance() {
        if (goodsRentBiz == null) {
            goodsRentBiz = new GoodsRentBiz();
        }
        return goodsRentBiz;
    }

    /**
     * 请求列表
     *
     * @param callback
     */
    public void requestGet(HashMap<String, String> map, VolleyHttpUtil.ResponseCallBack callback, String url) {
        VolleyHttpUtil.getInstance().getWithCallback(url, map, callback);
    }

    /**
     * 请求列表
     *
     * @param callback
     */
    public void requesPost(HashMap<String, String> urlMap, HashMap<String, Object> jsonMap, VolleyHttpUtil.ResponseCallBack callback, String url) {
        VolleyHttpUtil.getInstance().postWithCallback(url, urlMap, jsonMap, callback);
    }

    /**
     * put请求
     *
     * @param map
     * @param callback
     */
    public void requesPut(HashMap<String, Object> map, VolleyHttpUtil.ResponseCallBack callback, String url) {
        VolleyHttpUtil.getInstance().putWithCallback(url, map, callback);
    }


    /**
     * 详情回调
     *
     * @param response
     * @param errCode
     * @return
     */
    public Map<String, Object> handleOrderDetail(JSONObject response, int errCode) {
        Map<String, Object> bean = null;
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            Map<String, Object> map = JSONHelper
                    .jsonToMap(String.valueOf(response));
            bean = (Map<String, Object>) map.get("entity");
        }
        if (bean == null) {
            bean = JSONHelper
                    .jsonToMap(String.valueOf(response));
        }
        return bean;
    }

    /**
     * 地址拼接
     *
     * @param map
     * @return
     */
    private String assemblyRestUrl(String url, Map<String, Object> map) {
        String uri = url;
        if (map != null) {

            Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();

            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                if (entry.getKey().indexOf("{") > -1 || entry.getKey().indexOf("}") > -1) {
                    uri = uri.replace(entry.getKey(), entry.getValue().toString());
                }
            }

            return uri;
        } else {
            return uri;
        }

    }

    /**
     * 地址拼接2
     *
     * @param map
     * @return
     */
    private String assemblyUrl(Map<String, Object> map) {
        if (map != null) {
            StringBuffer sb_url = new StringBuffer();
            Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();

            while (it.hasNext()) {
                Map.Entry<String, Object> entry = it.next();
                if (entry.getKey().indexOf("{") > -1 || entry.getKey().indexOf("}") > -1) {
                    continue;
                }
                if (entry.getKey().equals("accesToken") || entry.getKey().equals("authToken") || entry.getKey().equals("Tag") || entry.getKey().equals("Dialog")) {
                    continue;
                }
                System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
                sb_url.append("&");
                sb_url.append(entry.getKey());
                sb_url.append("=");
                sb_url.append(entry.getValue());
            }
            if ("".equals(sb_url.toString())) return "";
            return sb_url.toString().substring(1);
        } else {
            return "";
        }

    }
}
